<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

<!-- datagrid数据表格 -->
<table id="myDatagridbanner"></table>
<!-- datagrid数据表格==END== -->

<!-- 按钮 -->
<div id="myBtnDivbanner">
    <shiro:hasRole name="superadmin">
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="toOpenAdd()">添加</a>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="toDelete()">批量删除</a>
    </shiro:hasRole>
</div>
<!-- 按钮===END=== -->


<!-- 添加对话框 -->
<div id="myAddDialogDivbanner" class="easyui-dialog" data-options="closed:true, width: 600, height: 400" >
    <form method="post" id="myFormbanner" enctype="multipart/form-data">
        图片：<input type="file" name="picture" /><br/><br/>
        名字：<input type="text" name="bannerOldName"/><br/><br/>
        描述：<input type="text" name="bannerDescription"/><br/><br/>
        <input type="button" value="添加" onclick="doAddSubmit()">
    </form>
</div>
<!-- 添加对话框===END=== -->


<!-- 修改 -->
<div id="myUpdateDialogDivbanner" class="easyui-dialog" data-options="closed:true, width: 600, height: 400">
    <form method="post" id="myUpdateFormbanner" enctype="multipart/form-data">
        <input type="hidden" name="bannerId" id="updatebannerId"><br/><br/>
        图片：<input type="file" name="picture" /><br/>
             <input type="hidden" name="bannerImageUrl" id="bannerImageUrl"/>
             <img src="" width="100" height="100" id="updatebannerImageUrl"/><br/><br/>
        名字：<input type="text" name="bannerOldName" id="updatebannerOldName"/><br/><br/>
        描述：<input type="text"  name="bannerDescription" id="updatebannerDescription"/><br/><br/>
        是否展示：<select name="bannerState">
                        <option value="1">展示</option>
                        <option value="2">不展示</option>
                    </select><br/><br/>
        <input type="button" value="修改" onclick="doUpdateSubmit()">
    </form>
</div>
<!-- 修改 ===END===-->

<script type="text/javascript">
    $(function (){

        /* datagrid展示数据 */
        $("#myDatagridbanner").datagrid({
            url:'${pageContext.request.contextPath}/banner/showAll',
            pagination:true,
            columns:[[
                {checkbox:true},
                {field:'bannerId',title:'编号'},
                {field:'bannerOldName',title:'名字'},
                {field:'bannerImageUrl',title:'图片',formatter: function(value,row,index){
                      return " <img src='${pageContext.request.contextPath}"+value+" 'width='30' height='30' />"
                   }
                },
                {field:'bannerDescription',title:'描述'},
                {field:'bannerDate',title:'上传日期'},
                {field:'bannerState',title:'是否展示',formatter: function(value,row,index){
                    if(value==1){
                        return "展示";
                    }else {
                        return "不展示";
                    }
                }}
            ]],
            toolbar: '#myBtnDivbanner',
            /* 双击修改 */
            <shiro:hasRole name="superadmin">
            onDblClickRow:function (rowIndex,rowData){
                $('#myUpdateDialogDivbanner').dialog("open");

                $("#updatebannerId").val(rowData.bannerId);
                $("#bannerImageUrl").val(rowData.bannerImageUrl);
                $("#updatebannerImageUrl").prop("src","${pageContext.request.contextPath}"+rowData.bannerImageUrl);
                $("#updatebannerOldName").val(rowData.bannerOldName);
                $("#updatebannerDescription").val(rowData.bannerDescription);
            }
            </shiro:hasRole>

        });
        /* datagrid展示数据  结束 */

    });



    /* 添加 */
    function toOpenAdd(){
        $('#myAddDialogDivbanner').dialog("open");
    };

    function doAddSubmit(){

        $('#myFormbanner').form('submit', {
            url:"${pageContext.request.contextPath}/banner/add",
            success:function(data){
                var data=JSON.parse(data);
                if(data.flag){
                    $('#myAddDialogDivbanner').dialog("close");
                    $("#myDatagridbanner").datagrid("reload");
                }else{
                    $.messager.alert('提示框','添加失败，请确认！','warning');
                }

            }
        });
    }
    /* 添加用户 */

    /* 批量删除 */
    function toDelete(){
        var allSelectedRows=$("#myDatagridbanner").datagrid("getSelections");
        var ids=new Array();

        for(var i=0;i<allSelectedRows.length;i++){
            ids[i]=allSelectedRows[i].bannerId;
        }

        $.ajax({
            url:"${pageContext.request.contextPath}/banner/deleteMany",
            data:"ids="+ids,
            type:"post",
            dataType:"JSON",
            success:function(data){
                if(data.flag){
                    $("#myDatagridbanner").datagrid("reload");
                }else{
                    $.messager.alert('提示框','删除失败，请确认！','warning');
                }
            }
        });
    }
    /* 批量删除 */

    /* 修改 */
    function doUpdateSubmit(){
        $('#myUpdateFormbanner').form('submit', {
            url:"${pageContext.request.contextPath}/banner/update",
            success:function(data){
                var data=JSON.parse(data);

                if(data.flag){
                    $("#myDatagridbanner").datagrid("reload");
                    $('#myUpdateDialogDivbanner').dialog("close");
                }else{
                    $.messager.alert('提示框','修改失败，请确认！','warning');
                }
            }
        });
    }
    /* 修改 ===END===*/

</script>